﻿using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using RecruitStudents.Client.BLL;
using RecruitStudents.Utility;
using RecruitStudents.Utility.Const;
using RecruitStudents.Utility.Helper;
using RecruitStudents.Utility.Json;
using RecruitStudents.Utility.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RecruitStudents.Client.Financial
{
    public partial class Financial_OnSitePayFeeForm_List : BaseForm
    {
        ServiceFinancial service = new ServiceFinancial();
        ServiceExaminee serviceExam = new ServiceExaminee();
        ServiceComm bllCommService = new ServiceComm();
        private string strExamCity = SessionerHelper.ExamPlaceID;//考点
        public Financial_OnSitePayFeeForm_List()
        {
            InitializeComponent();
            #region 绑定生源地下拉列表框
            DataTable dtExamArea = bllCommService.GetDicDataTableByDicTableName("TB_Dic_ExamProvince");
            if (dtExamArea != null)
            {
                this.comboExamProvince.Properties.Columns.Clear();
                this.comboExamProvince.Properties.Columns.Add(new LookUpColumnInfo("DicNO", 30, "生源地代码"));
                this.comboExamProvince.Properties.Columns.Add(new LookUpColumnInfo("DicName", 55, "生源地名称"));
                this.comboExamProvince.Properties.ValueMember = "DicNO";
                this.comboExamProvince.Properties.DisplayMember = "DicName";
                this.comboExamProvince.Properties.DataSource = dtExamArea;
                this.comboExamProvince.Properties.NullText = "";
            }
            #endregion
            userControl_ReadIdCard1.TextEnabled = true;
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            BoundData("1");
        }
        #region 绑定报考专业信息
        private void BindSpecialityList(string strIdCard)
        {
            using (DataTable dt = service.GetExameeSpeciality_ExamLevelBySignUpID(strIdCard))
            {
                if (dt != null)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        dr["IsPass"] = serviceExam.GetExamLog(dr["SignUpID"].ToSafeString(), dr["SpecialityKindsNO"].ToString());
                    }
                }
                //绑定专业信息列表
                gridSpeciality.DataSource = dt;
            }
        }
        #endregion

        #region 加载数据
        private void BoundData(string flag)
        {
            BindSpecialityList(userControl_ReadIdCard1.TextValue);
            using (DataTable dt = service.GetExamineePayFee(strExamCity, userControl_ReadIdCard1.TextValue.Trim()))
            {
                decimal sumMonery = 0;
                decimal money = 0;
                if (dt != null && dt.Rows.Count > 0)
                {
                    dt.Columns.Add("check", typeof(bool));
                    foreach (DataRow dr in dt.Rows)
                    {
                        dr["check"] = true;
                        if (!string.IsNullOrEmpty(dr["ExamFee"].ToString()) && !string.IsNullOrEmpty(dr["CounterFee"].ToString()))
                        {
                            money = Convert.ToDecimal(dr["ExamFee"].ToString()) + Convert.ToDecimal(dr["CounterFee"].ToString());
                            sumMonery += money;
                        }
                    }
                    gridControl1.DataSource = dt;
                    double decMoney = Convert.ToDouble(sumMonery);
                    lblAmont.Text = decMoney.ToSafeString();

                    txtNMT_SignUpID.Text = dt.Rows[0]["NMT_SignUpID"].ToString().Trim();
                    txtIdentityCard.Text = dt.Rows[0]["IdentityCard"].ToString().Trim();
                    txtName.Text = dt.Rows[0]["Name"].ToString().Trim();
                    comboExamProvince.EditValue = dt.Rows[0]["ExamProvince"].ToString().Trim();
                    userControl_ReadIdCard1.TextValue = "";
                    btnPayFee.Focus();
                }
                else
                {
                    if (flag != "2")
                    {
                        DevHelper.ShowWarningMessageBox("没有查到该考生需要缴纳的考试费用!");
                    }
                    gridControl1.DataSource = null;
                    gridSpeciality.DataSource = null;
                    txtIdentityCard.Text = string.Empty;
                    txtNMT_SignUpID.Text = string.Empty;
                    comboExamProvince.EditValue = "";
                    txtName.Text = string.Empty;
                    lblAmont.Text = "0.00";
                    userControl_ReadIdCard1.TextValue = "";
                    return;
                }
            }
        }
        #endregion

        #region 判断是否是有  选择专业
        /// <summary>
        /// 判断是否是有  选择专业
        /// </summary>
        /// <returns></returns>
        private int IsSelectSpeciality(out decimal sumMoney)
        {
            sumMoney = 0;
            int iCount = 0;
            for (int i = 0; i < gridView1.RowCount; i++)
            {
                string val = gridView1.GetRowCellValue(i, gridView1.Columns[0]).ToString();
                if (val != null && val.ToLower() == "true")
                {
                    string strExamFee = gridView1.GetRowCellValue(i, gridView1.Columns["ExamFee"]).ToString();
                    string strExamCounterFee = gridView1.GetRowCellValue(i, gridView1.Columns["CounterFee"]).ToString();
                    if (!string.IsNullOrEmpty(strExamFee) && !string.IsNullOrEmpty(strExamCounterFee))
                    {
                        sumMoney += (strExamFee.ToSafeDecimal() + strExamCounterFee.ToSafeDecimal());
                    }
                    iCount++;
                }
            }
            return iCount;
        }
        #endregion 

        #region 缴费确认
        private void btnPayFee_Click(object sender, EventArgs e)
        {
            if (txtIdentityCard.Text == "")
            {
                DevHelper.ShowWarningMessageBox("请先刷身份证号或输入身份证号查询再进行缴费！");
                return;
            }
            if (gridView1.DataSource == null || gridView1.RowCount == 0)
            {
                DevHelper.ShowWarningMessageBox("请确认该考生是否有未缴费订单！");
                return;
            }
            decimal sumMoney = 0;
            if (IsSelectSpeciality(out sumMoney) <= 0)
            {
                DevHelper.ShowWarningMessageBox("请选择需要缴费的专业！");
                return;
            }
            TB_SignUpStudentsNetPayFee_Head modelFee = null;
            int iCount = gridView1.RowCount;
            if (iCount > 0)
            {
                modelFee = GetModelOrderHead(sumMoney);
            }
            if (modelFee == null)
            {
                DevHelper.ShowWarningMessageBox("请选择需要缴费的专业！");
                return;
            }
            List<TB_Exam_SignUpStudentsSpeciality> StudentsSpeciality = new List<TB_Exam_SignUpStudentsSpeciality>();
            List<TB_SignUpStudentsNetPayFee_Detail> PayFee_DetailList = new List<TB_SignUpStudentsNetPayFee_Detail>();

            decimal Sum_Charge = 0;
            for (int i = 0; i < gridView1.RowCount; i++)
            {
                string strSignUpStudentsSpecialityID = gridView1.GetRowCellValue(i, gridView1.Columns["SignUpStudentsSpecialityID"]).ToString();
                string strSpecialityKindsNO = gridView1.GetRowCellValue(i, gridView1.Columns["SpecialityKindsNO"]).ToString();
                string strExamTime = serviceExam.GetExamTime(strExamCity, strSpecialityKindsNO);
                string strExamLevel = gridView1.GetRowCellValue(i, gridView1.Columns["ExamLevel"]).ToString();
                string val = gridView1.GetRowCellValue(i, gridView1.Columns[0]).ToSafeString().ToLower();
                TB_Exam_SignUpStudentsSpeciality model = new TB_Exam_SignUpStudentsSpeciality();
                if (val == "true")
                {
                    model.ExamLevel = strExamLevel.ToSafeInt();
                    model.SignUpStudentsSpecialityID = strSignUpStudentsSpecialityID;
                }
                TB_SignUpStudentsNetPayFee_Detail modelDetailFee = new TB_SignUpStudentsNetPayFee_Detail();
                if (val == "true" && strExamLevel == "2")
                {
                    model.IsFee_Second = ConstAppConfig.YJF_Value;
                    model.AppointmentGroup_Second = strExamTime;
                    model.AppointmentGroupTime_Second = DateTime.Now;
                    model.OrderNo_Second = modelFee.OrdNO;

                    modelDetailFee.OrdNO = modelFee.OrdNO;
                    modelDetailFee.SpecialityKindsNO = strSpecialityKindsNO;
                    modelDetailFee.ExamLevel = strExamLevel.ToSafeInt();
                    modelDetailFee.Fee = gridView1.GetRowCellValue(i, gridView1.Columns["ExamFee"]).ToString().ToSafeDecimal();
                    modelDetailFee.CounterFee = gridView1.GetRowCellValue(i, gridView1.Columns["CounterFee"]).ToString().ToSafeDecimal();
                    modelDetailFee.ExamYear = SessionerHelper.SystemYear;
                    modelDetailFee.ExamPlaceNO = SessionerHelper.ExamPlaceID;
                    modelDetailFee.ChargeUserID = SessionerHelper.UserID;
                    modelDetailFee.ChargeIP = SessionerHelper.UserLoginIP;
                    modelDetailFee.ExamTime = strExamTime;
                    Sum_Charge = Sum_Charge + modelDetailFee.Fee + modelDetailFee.CounterFee.ToSafeDecimal();
                }
                else if (val == "true" && strExamLevel == "3")
                {
                    model.IsFee_Third = ConstAppConfig.YJF_Value;
                    model.AppointmentGroup_Third = strExamTime;
                    model.AppointmentGroupTime_Third = DateTime.Now;
                    model.OrderNo_Third = modelFee.OrdNO;

                    modelDetailFee.OrdNO = modelFee.OrdNO;
                    modelDetailFee.SpecialityKindsNO = strSpecialityKindsNO;
                    modelDetailFee.ExamLevel = strExamLevel.ToSafeInt();
                    modelDetailFee.Fee = gridView1.GetRowCellValue(i, gridView1.Columns["ExamFee"]).ToString().ToSafeDecimal();
                    modelDetailFee.CounterFee = gridView1.GetRowCellValue(i, gridView1.Columns["CounterFee"]).ToString().ToSafeDecimal();
                    modelDetailFee.ExamYear = SessionerHelper.SystemYear;
                    modelDetailFee.ExamPlaceNO = SessionerHelper.ExamPlaceID;
                    modelDetailFee.ChargeUserID = SessionerHelper.UserID;
                    modelDetailFee.ChargeIP = SessionerHelper.UserLoginIP;
                    modelDetailFee.ExamTime = strExamTime;
                    Sum_Charge = Sum_Charge + modelDetailFee.Fee + modelDetailFee.CounterFee.ToSafeDecimal();
                }
                else if (val == "true" && strExamLevel == "4")
                {
                    model.IsFee_Fourth = ConstAppConfig.YJF_Value;
                    model.AppointmentGroup_Fourth = strExamTime;
                    model.AppointmentGroupTime_Fourth = DateTime.Now;
                    model.OrderNo_Fourth = modelFee.OrdNO;

                    modelDetailFee.OrdNO = modelFee.OrdNO;
                    modelDetailFee.SpecialityKindsNO = strSpecialityKindsNO;
                    modelDetailFee.ExamLevel = strExamLevel.ToSafeInt();
                    modelDetailFee.Fee = gridView1.GetRowCellValue(i, gridView1.Columns["ExamFee"]).ToString().ToSafeDecimal();
                    modelDetailFee.CounterFee = gridView1.GetRowCellValue(i, gridView1.Columns["CounterFee"]).ToString().ToSafeDecimal();
                    modelDetailFee.ExamYear = SessionerHelper.SystemYear;
                    modelDetailFee.ExamPlaceNO = SessionerHelper.ExamPlaceID;
                    modelDetailFee.ChargeUserID = SessionerHelper.UserID;
                    modelDetailFee.ChargeIP = SessionerHelper.UserLoginIP;
                    modelDetailFee.ExamTime = strExamTime;
                    Sum_Charge = Sum_Charge + modelDetailFee.Fee + modelDetailFee.CounterFee.ToSafeDecimal();
                }
                else if (val == "true" && strExamLevel == "5")
                {
                    model.IsFee_Fifth = ConstAppConfig.YJF_Value;
                    model.AppointmentGroup_Fifth = strExamTime;
                    model.AppointmentGroupTime_Fifth = DateTime.Now;
                    model.OrderNo_Fifth = modelFee.OrdNO;

                    modelDetailFee.OrdNO = modelFee.OrdNO;
                    modelDetailFee.SpecialityKindsNO = strSpecialityKindsNO;
                    modelDetailFee.ExamLevel = strExamLevel.ToSafeInt();
                    modelDetailFee.Fee = gridView1.GetRowCellValue(i, gridView1.Columns["ExamFee"]).ToString().ToSafeDecimal();
                    modelDetailFee.CounterFee = gridView1.GetRowCellValue(i, gridView1.Columns["CounterFee"]).ToString().ToSafeDecimal();
                    modelDetailFee.ExamYear = SessionerHelper.SystemYear;
                    modelDetailFee.ExamPlaceNO = SessionerHelper.ExamPlaceID;
                    modelDetailFee.ChargeUserID = SessionerHelper.UserID;
                    modelDetailFee.ChargeIP = SessionerHelper.UserLoginIP;
                    modelDetailFee.ExamTime = strExamTime;
                    Sum_Charge = Sum_Charge + modelDetailFee.Fee + modelDetailFee.CounterFee.ToSafeDecimal();
                }
                else if (val == "true" && strExamLevel == "6")
                {
                    model.IsFee_Sixth = ConstAppConfig.YJF_Value;
                    model.AppointmentGroup_Sixth = strExamTime;
                    model.AppointmentGroupTime_Sixth = DateTime.Now;
                    model.OrderNo_Sixth = modelFee.OrdNO;

                    modelDetailFee.OrdNO = modelFee.OrdNO;
                    modelDetailFee.SpecialityKindsNO = strSpecialityKindsNO;
                    modelDetailFee.ExamLevel = strExamLevel.ToSafeInt();
                    modelDetailFee.Fee = gridView1.GetRowCellValue(i, gridView1.Columns["ExamFee"]).ToString().ToSafeDecimal();
                    modelDetailFee.CounterFee = gridView1.GetRowCellValue(i, gridView1.Columns["CounterFee"]).ToString().ToSafeDecimal();
                    modelDetailFee.ExamYear = SessionerHelper.SystemYear;
                    modelDetailFee.ExamPlaceNO = SessionerHelper.ExamPlaceID;
                    modelDetailFee.ChargeUserID = SessionerHelper.UserID;
                    modelDetailFee.ChargeIP = SessionerHelper.UserLoginIP;
                    modelDetailFee.ExamTime = strExamTime;
                    Sum_Charge = Sum_Charge + modelDetailFee.Fee + modelDetailFee.CounterFee.ToSafeDecimal();
                }
                if (!string.IsNullOrEmpty(modelDetailFee.OrdNO))
                {
                    PayFee_DetailList.Add(modelDetailFee);
                }
                if (val.ToLower() == "true")
                {
                    StudentsSpeciality.Add(model);
                }
            }
            if (modelFee != null)
            {
                modelFee.Sum_Charge = Sum_Charge;
                modelFee.Total_fee = modelFee.Sum_Charge;
                modelFee.Price = modelFee.Sum_Charge;
            }

            if (XtraMessageBox.Show("请确认缴费金额：" + sumMoney, "缴费确认", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                JsonMessage jsonMessage = service.UpdateSpecialityInfoState(modelFee, PayFee_DetailList, StudentsSpeciality);
                if (jsonMessage.State == "true")
                {
                    DevHelper.ShowInfoMessageBox("缴费成功！");
                    userControl_ReadIdCard1.TextFocus();
                    gridControl1.DataSource = null;
                    gridSpeciality.DataSource = null;
                    txtIdentityCard.Text = string.Empty;
                    txtNMT_SignUpID.Text = string.Empty;
                    comboExamProvince.EditValue = "";
                    txtName.Text = string.Empty;
                    lblAmont.Text = "0.00";
                }
                else
                {
                    DevHelper.ShowErrorMessageBox("缴费失败！");
                }
            }
        }

        private TB_SignUpStudentsNetPayFee_Head GetModelOrderHead(decimal sumMoney)
        {
            TB_SignUpStudentsNetPayFee_Head modelFee = new TB_SignUpStudentsNetPayFee_Head();
            modelFee.SignUpID = gridView1.GetRowCellValue(0, gridView1.Columns["SignUpID"]).ToString();
            modelFee.OrdNO = bllCommService.CreateOrderNo();
            modelFee.PaySource = ConstPaySource.XJ;//现金
            modelFee.Sum_Charge = sumMoney;
            modelFee.Total_fee = modelFee.Sum_Charge;
            modelFee.Price = modelFee.Sum_Charge;
            modelFee.ChargeUserID = SessionerHelper.UserID;
            modelFee.ChargeIP = SessionerHelper.UserLoginIP;
            modelFee.ExamYear = SessionerHelper.SystemYear;
            modelFee.ChargDate = DateTime.Now;
            modelFee.IsFee = "1";
            modelFee.IsFeeSuccess = "1";
            return modelFee;
        }
        #endregion
    }
}
